import 'package:loxen/api/http_server/dictionaries.dart';
import 'package:loxen/api/http_server/file_change_delete_add_select.dart';
import 'package:loxen/api/http_util.dart';
import 'package:loxen/mode/master/file_list_bean.dart';
import 'package:loxen/mode/master/file_loxen.dart';
import 'package:rxdart/rxdart.dart';
import 'package:loxen/api/api.dart';
import 'package:loxen/viewmode/base_view_model.dart';

class CollectionFileAddViewModel extends BaseViewModel
    with FileChangeDeleteAddSelectMixin {
  final FileType fileType;

  int page = 1;

  FileListBean _bean;

  List<FileLoxen> _list;

  List<FileLoxen> get list => _list;

  CollectionFileAddViewModel(this.fileType);

  void collection(String id,
      {Function() onSuccess, Function(String error) onError}) {
    _list.forEach((element) {
      if (element.select == 1) {
        ApiService.collection(getUrl(), element.id.toString(), instanceId: id)
            .listen((event) {
          if (event.statusCode == 201) {
            if (onSuccess != null) onSuccess.call();
          } else {
            if (onError != null) onError.call('收藏失败');
          }
        }, onError: (error, stack) {
          if (error is ServiceException) {
            if (onError != null) onError.call(error.data.toString());
          } else {
            if (onError != null) onError.call(error.toString());
          }
        });
      }
    });
  }

  void createFile(String name, bool isHide) {
    fileAdd(name, getType(), isHide).listen((event) {
      if (event != null && event > 0) {
        _list = _list.take(_list.length).toList();
        _list.add(FileLoxen(
          id: event,
          name: name,
          num: 0,
          canEdit: 1,
          cover: '',
          isHide: isHide,
          myHide: isHide,
        ));
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  void changeSelect(FileLoxen fileLoxen) {
    _list.forEach((element) => element.select = 0);
    fileLoxen.select = 1;
    notifyListeners();
  }

  void nextData() {
    if (_bean.data.next.trim() == '') return;
    page += 1;
    fileSelect(getType(), page: page.toString()).listen((event) {
      if (event != null) {
        _bean = event;
        _list = _list.take(_list.length);
        _list.addAll(event.data.results);
        notifyListeners();
      }
    }, onError: (error, stack) {
      print(error);
      print(stack);
    });
  }

  void loadData() {
    fileSelect(getType())
        .doOnListen(() {
          mState = BaseState.LOADING;
          notifyListeners();
        })
        .listen((event) {
          if (event != null &&
              event.data != null &&
              event.data.results != null &&
              event.data.results.length > 0) {
            mState = BaseState.CONTENT;
            _bean = event;
            _list = event.data.results;
            _list[0].select = 1;
          } else {
            mState = BaseState.EMPTY;
          }
        }, onError: (error, stack) {
          print(error);
          print(stack);
          mState = BaseState.FAIL;
        }, onDone: () => notifyListeners());
  }

  //    ('1', '灵感'),
  //    ('2', '素材'),
  //    ('3', '采集'),
  //    ('4', '知识'),
  //    ('5', '应用'),
  //    ('6', '书本'),
  String getType() {
    if (fileType == FileType.INSPIRATION) {
      return '1';
    }
    if (fileType == FileType.MATERIAL) {
      return '2';
    }
    if (fileType == FileType.IMAGE) {
      return '3';
    }
    if (fileType == FileType.KNOWLEDGE) {
      return '4';
    }
    if (fileType == FileType.APPLY) {
      return '5';
    }
    return '';
  }

  ///灵感：idea-article
  ///知识：study-knowledge
  ///采集：idea-picture
  ///书本：study-book
  ///应用：study-case
  ///素材：material-product

  String getUrl() {
    if (fileType == FileType.INSPIRATION) {
      return 'idea-article';
    }
    if (fileType == FileType.MATERIAL) {
      return 'material-product';
    }
    if (fileType == FileType.IMAGE) {
      return 'idea-picture';
    }
    if (fileType == FileType.KNOWLEDGE) {
      return 'study-knowledge';
    }
    if (fileType == FileType.APPLY) {
      return 'study-case';
    }
    return "";
  }

  static FileListBean decodeFileListBean(dynamic json) {
    return FileListBean.fromJson(json);
  }
}
